//------------------------------------------------------------------------------
//  EffectVariable.cpp
//  (C) 2009 xoyojank
//------------------------------------------------------------------------------
#include "StdEddy.h"
#include "Graphics/EffectVariable.h"

//------------------------------------------------------------------------------
namespace Eddy
{

//------------------------------------------------------------------------------
EffectVariable::EffectVariable()
    : type(UnknowType)
    , intValue(0)
    , location(-1)
    , textureType(GL_TEXTURE_2D)
{
    // empty
}

//------------------------------------------------------------------------------
EffectVariable::~EffectVariable()
{
    this->Clear();
}

//------------------------------------------------------------------------------
void
EffectVariable::Clear()
{
    switch (this->type)
    {
    case VectorType:
        delete this->vectorValue;
        break;
    case Float4Type:
        delete this->float4Value;
        break;
    case MatrixType:
        delete this->matrixValue;
        break;
    }
    this->intValue = 0;
}

//------------------------------------------------------------------------------
void
EffectVariable::SetType(GLenum t)
{
    switch (t)
    {
    case GL_INT:
        this->type = IntType;
        break;
    case GL_FLOAT:
        this->type = FloatType;
        break;
    case GL_FLOAT_VEC3:
        this->vectorValue = new vector();
        this->type = VectorType;
        break;
    case GL_FLOAT_VEC4:
        this->float4Value = new float4();
        this->type = Float4Type;
        break;
    case GL_FLOAT_MAT4:
        this->matrixValue = new matrix44();
        this->type = MatrixType;
        break;
    case GL_SAMPLER_2D:
    case GL_SAMPLER_CUBE:
        this->type = TextureType;
        break;
    default:
        assert(false); // unsupported now!
    }
}

//------------------------------------------------------------------------------
void
EffectVariable::Commit(GLint texIndex)
{
    assert(UnknowType != this->type);
    assert(0 != this->intValue);
    assert(-1 != this->location);

    switch (this->type)
    {
    case IntType:
        glUniform1i(this->location, this->intValue);
        break;
    case FloatType:
        glUniform1f(this->location, this->floatValue);
        break;
    case VectorType:
        glUniform3fv(this->location, 1, (float*)&this->vectorValue);
        break;
    case Float4Type:
        glUniform4fv(this->location, 1, (float*)&this->float4Value);
        break;
    case MatrixType:
        glUniformMatrix4fv(this->location, 1, GL_FALSE, (float*)&this->matrixValue);
        break;
    case TextureType:
        glActiveTexture(GL_TEXTURE0 + texIndex);
        glBindTexture(this->textureType, this->texture);
        glUniform1i(this->location, texIndex);
        break;
    }
}

}// Eddy
